package com.yorick.account.service.impl;

import com.yorick.account.dao.RegisterMapper;
import com.yorick.account.modal.request.AccountAddRequest;
import com.yorick.account.service.RegisterService;
import com.yorick.common.Result.SimpleFactory.ResultFactory;
import com.yorick.common.error.YorickAssert;
import com.yorick.common.util.Encrypt;
import com.yorick.account.entity.AccountDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    RegisterMapper registerMapper;

    @Override
    public Map<String, Object> addAccount(AccountAddRequest request) {
        AccountDto accountDto = AccountDto.builder()
                .account(request.getAccount())
                .mobilephone(request.getMobilephone())
                .email(request.getEmail())
                .username(request.getUsername())
                .build();
        List<AccountDto> isExist = registerMapper.isAccountExist(accountDto);
        YorickAssert.validate(
                isExist.size() == 0,
                () -> "0",
                () -> "该账号已存在");
        //添加用户信息
        String password = Encrypt.encrypt(request.getAccount() + request.getPassword());
        accountDto.setPassword(password);
        registerMapper.insertAccount(accountDto);
        return ResultFactory.success().display();
    }

}
